<script setup lang="ts">
import type { KnowledgeList, KnowledgeParams, KnowledgeType } from '@/types/consult'
import KnowledgeCard from './KnowledgeCard.vue'
import { ref } from 'vue'
import { getKnowledgePage } from '@/services/consult'

// 接收文章知识类型
const props = defineProps<{
  type: KnowledgeType
}>()

// 当前加载显示的知识列表
const list = ref<KnowledgeList>([])
// 查询数据
const queryParams: KnowledgeParams = {
  type: props.type,
  current: 1,
  pageSize: 10
}
const loading = ref<boolean>(false)
const finished = ref<boolean>(false)
// 触底时加载更多文章列表数据
async function onLoad() {
  const { data } = await getKnowledgePage(queryParams)
  // 当前次请求的数据和上次的数据进行合并
  list.value = [...list.value, ...data.rows]
  // 当前次数据加载完毕后需要将loading手动设置为false
  loading.value = false
  // 页码+1
  queryParams.current += 1
  // 判断临界点
  // 总共95条数据->total，每页显示10条，总页数10
  if (queryParams.current > data.pageTotal) {
    finished.value = true
  }
}

</script>

<template>
  <div class="knowledge-list">
    <van-list v-model:loading="loading" :finished="finished" finished-text="没有更多了" @load="onLoad">
      <knowledge-card v-for="item in list" :key="item.id" :item="item"></knowledge-card>
    </van-list>
  </div>
</template>

<style lang="scss" scoped>
.knowledge-list {
  padding: 0 15px;
}
</style>